package com.rx.action;

import java.io.Serializable;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.reflection.ReflectionUtils;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.rx.biz.BaseBiz;
import com.rx.common.Constants;
import com.rx.common.exception.ValidateException;
import com.rx.entity.BaseEntity;

public abstract class BaseAction<T, PK extends Serializable> extends
		ActionSupport implements ModelDriven<T>, Preparable {

	private static final long serialVersionUID = -1653204626115064950L;

	public static final String RELOAD = "reload";

	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected BaseBiz<T, PK> biz;

	public abstract void setBiz(BaseBiz<T, PK> biz);

	protected PK id;

	public void setId(PK id) {
		this.id = id;
	}

	protected T entity;

	public T getModel() {
		return entity;
	}

	protected Page<T> page = new Page<T>(Constants.WEBPAGE_RECORD_NUMBER);

	public Page<T> getPage() {
		return page;
	}

	@SuppressWarnings("unchecked")
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = biz.get(id);
		} else {
			entity = (T) ReflectionUtils.getSuperClassGenricType(getClass())
					.newInstance();
		}
	}

	@Override
	public String execute() throws Exception {
		return list();
	}

	public abstract String list() throws Exception;

	public abstract String input() throws Exception;

	public abstract String save() throws Exception;

	public abstract String delete() throws Exception;

	public void prepare() throws Exception {
	}

	public void prepareInput() throws Exception {
		prepareModel();
	}

	public void prepareSave() throws Exception {
		prepareModel();
	}

	protected HttpServletRequest getRequest() {
		return Struts2Utils.getRequest();
	}

	protected HttpSession getSession() {
		return Struts2Utils.getSession();
	}

	protected String getParameter(String key) {
		return getRequest().getParameter(key);
	}

	protected String[] getParameterValues(String key) {
		return getRequest().getParameterValues(key);
	}

	protected void setAttribute(String key, Object value) {
		getRequest().setAttribute(key, value);
	}

	protected String currentUserName() {
		return (String) getSession().getAttribute("currentUserName");
	}

	protected void setBaseProperty(BaseEntity baseEntity) {
		if (baseEntity.getId() == null) {
			// 添加
			if (baseEntity.getIndexOrder() == null) {
				baseEntity.setIndexOrder(System.currentTimeMillis());
			}
			baseEntity.setCreateUser(currentUserName());
			baseEntity.setCreateDate(new Date());
			baseEntity.setUpdateUser(currentUserName());
			baseEntity.setUpdateDate(new Date());
		} else {
			// 修改
			baseEntity.setUpdateUser(currentUserName());
			baseEntity.setUpdateDate(new Date());
		}
	}

	protected void isValidate(Object[] fields, String[] means) {
		Boolean sign = false;
		for (int i = 0; i < fields.length; i++) {
			Object obj = fields[i];
			if (obj == null) {
				addActionMessage(means[i] + "不允许为空！");
				sign = true;
			}
			if (obj instanceof String && StringUtils.isEmpty(obj.toString())) {
				addActionMessage(means[i] + "不允许为空！");
				sign = true;
			}
			if (obj instanceof String[] && ArrayUtils.isEmpty((String[]) obj)) {
				addActionMessage(means[i] + "不允许为空！");
				sign = true;
			}
		}
		if (sign) {
			throw new ValidateException();
		}
	}

}
